package com.wj.console.common.Interceptor;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.base.util.SessionUtils;
import com.wj.console.constant.Constant;

/**
 * 
 * @ClassName: PermissionsInterceptor
 * @Description: 企业登录用户权限拦截器
 * @author wj
 * @date 2016年6月3日 上午11:48:57
 *
 */
public class PermissionsInterceptor implements HandlerInterceptor {

	private final static Logger log = Logger.getLogger(PermissionsInterceptor.class);

	@Autowired
	private Constant constant;
	
	private List<String> exclusions;
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String path = request.getServletPath();
		if (exclusions != null && !exclusions.isEmpty()) {
		    if (exclusions.contains(path)) {
		        return true;
		    }
		}
		try {
			@SuppressWarnings("unchecked")
            Map<String, String> roleMap = (Map<String, String>) SessionUtils.getAttr(request, "roleMap");
			if(roleMap == null){
			    //return true;
				response.setStatus(HttpStatus.FORBIDDEN.value());
				response.sendError(HttpStatus.FORBIDDEN.value());
				return false;
			} else {
			    if (!roleMap.containsKey(path)) {
	                log.error("没有权限访问Url:" + path);
	                response.setStatus(HttpStatus.FORBIDDEN.value());
	                response.sendError(HttpStatus.FORBIDDEN.value());
	                return false;
	            }
			}
			
		} catch (Exception e) {
			response.sendRedirect(constant.getDomainName());
			return false;
		}
		return true;
	}

    public void setExclusions(List<String> exclusions) {
        this.exclusions = exclusions;
    }

    /* (non-Javadoc)
     * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        // TODO Auto-generated method stub
        
    }

    /* (non-Javadoc)
     * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        // TODO Auto-generated method stub
        
    }

}
